<?php


namespace Zhifubao;


use Zhifubao\Request\AccountBookCreateRequest;
use Zhifubao\Request\AccountBookQueryRequest;
use Zhifubao\Request\AccountBookRechargeQueryRequest;
use Zhifubao\Request\AccountBookRechargeRequest;
use Zhifubao\Request\AccountBookSignRequest;
use Zhifubao\Request\AccountQueryRequest;
use Zhifubao\Request\AlipayFundTransUniTransferRequest;

class AccountClient extends Client
{

    public function signForm($external_agreement_no)
    {
        $request = new AccountBookSignRequest();
        $request->setBizContent(json_encode([
            'personal_product_code' => 'FUND_SAFT_SIGN_WITHHOLDING_P',
            'sign_scene' => 'INDUSTRY|SATF_ACC',
            'access_params' => ["channel" => "QRCODE"],
            'product_code' => 'FUND_SAFT_SIGN_WITHHOLDING',
            'external_agreement_no' => $external_agreement_no,
            'third_party_type' => 'PARTNER'
        ], true));
        return $this->pageRequest($request, 'get');
    }

    public function querySign($external_agreement_no)
    {
        $request = new AccountBookQueryRequest();

        $request->setBizContent(json_encode([
            'personal_product_code' => 'FUND_SAFT_SIGN_WITHHOLDING_P',
            'sign_scene' => 'INDUSTRY|SATF_ACC',
            'product_code' => 'FUND_SAFT_SIGN_WITHHOLDING',
            'external_agreement_no' => $external_agreement_no,
        ], true));
        return $this->request($request);
    }


    public function createAccount($agreement_no, $merchant_user_id)
    {
        $request = new AccountBookCreateRequest();
        $request->setBizContent(json_encode([
            'merchant_user_id' => $merchant_user_id,
            'merchant_user_type' => 'BUSINESS_EMPLOYEE',
            'scene_code' => 'SATF_FUND_BOOK',
            'ext_info' => ['agreement_no' => $agreement_no]
        ], true));
        return $this->request($request);
    }

    public function queryAccount($account_book_id, $agreement_no)
    {
        $request = new AccountQueryRequest();
        $request->setBizContent(json_encode([
            'account_book_id' => $account_book_id,
            'scene_code' => 'SATF_FUND_BOOK',
            'ext_info' => ['agreement_no' => $agreement_no]
        ], true));
        return $this->request($request);
    }

    public function rechargeForm($out_biz_no, $amount, $time_expire, $account_book_id, $agreement_no)
    {
        $request = new AccountBookRechargeRequest();
        $request->setBizContent(json_encode([
            'out_biz_no' => $out_biz_no,
            'trans_amount' => $amount,
            'product_code' => 'FUND_ACCOUNT_BOOK',
            'biz_scene' => 'SATF_DEPOSIT',
            'time_expire' => $time_expire,//2022-05-10 18:00
            'payee_info' => ['identity_type' => 'ACCOUNT_BOOK_ID', 'identity' => $account_book_id, 'ext_info' => '{"agreement_no":"' . $agreement_no . '"}'],
        ], true));
        return $this->pageRequest($request);
    }


    public function rechargeQuery($out_biz_no)
    {
        $request = new AccountBookRechargeQueryRequest();
        $request->setBizContent(json_encode([
            'out_biz_no' => $out_biz_no,

            'product_code' => 'FUND_ACCOUNT_BOOK',
            'biz_scene' => 'SATF_DEPOSIT',
        ], true));
        return $this->request($request);
    }


    public function pay($out_biz_no, $amount, $account_book_id, $agreement_no, $zfbAccount, $zfbAccountName)
    {
        $request = new AlipayFundTransUniTransferRequest();
        $request->setBizContent(json_encode([
            'out_biz_no' => $out_biz_no,
            'trans_amount' => $amount,
            'product_code' => 'SINGLE_TRANSFER_NO_PWD',
            'biz_scene' => 'ENTRUST_TRANSFER',
            'business_params' => '{"payer_show_name_use_alias":"true"}',
            'payee_info' => [
                'identity' => $zfbAccount,
                'identity_type' => 'ALIPAY_LOGON_ID',
                'name' => $zfbAccountName,
            ],
            'payer_info' => [
                'identity' => $account_book_id,
                'identity_type' => 'ACCOUNT_BOOK_ID',
                'ext_info' => '{"agreement_no":"' . $agreement_no . '"}',
            ],
        ], true));
        return $this->request($request);
    }
}
